// pages/detail/index.js
import { dateFormat } from '../../utils/dateFormat.js'
import { timeTransform } from '../../utils/timeTransform.js'
const app = getApp()
let rightClickState = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    voice: {},
    commentList: [],
    currentPage: 1,
    isDownLoading: true,
    loadingUp: false,
    replying: false,
    replyContent: ''
    // hasContent: false
    // allContent: '',
    // bgImg: ''
  },

  lookHome(e) {
    wx.navigateTo({
      url: '/pages/personalHome/index?openid=' + e.currentTarget.dataset.openid,
    })
  },

  lookImg(e) { //图片预览
    wx.previewImage({
      urls: [e.currentTarget.dataset.img],
      current: e.currentTarget.dataset.img
    })
  },

  getBothRight(){
    if (rightClickState){
      return
    }else{
      rightClickState = true
    }
    let rightId = this.data.commentList[this.data.operateIndex].commentatorId
    wx.showLoading({
      title: '处理中……',
    })
    wx.cloud.callFunction({
      name: 'operateRight',
      data: {
        openid: rightId
      }
    }).then(res => {
      this.setData({
        modalName: null
      })
      rightClickState = false
      switch (res.result.state){
        case 0: 
          wx.showToast({
            title: '成功申请，30日剩余次数：' + res.result.restCount + '/3',
            icon: 'none',
            duration: 3000
          })
        break
        case 1:
          wx.showToast({
            title: '正在申请中',
            icon: 'success',
            duration: 2000
          })
          break
        case 2:
          wx.showToast({
            title: '已互通！请勿重复申请！',
            icon: 'none',
            duration: 2000
          })
        break
        case 3:
          wx.showToast({
            title: '30日内剩余申请次数：' + res.result.restCount + '/3',
            icon: 'none',
            duration: 3000
          })
        break
        case 4:
          wx.showToast({
            title: '30日申请次数已达最高限制！',
            icon: 'none',
            duration: 2000
          })
        break
        case 5:
          wx.showToast({
            title: '成功与Ta互通海螺！',
            icon: 'success',
            duration: 2000
          })
        break
        case 6:
          wx.showToast({
            title: '请先填写自己的互通信息！',
            icon: 'none',
            duration: 2000
          })
        break
        case 7:
          wx.showToast({
            title: '对方未完成互通信息填写，无法发起申请！',
            icon: 'none',
            duration: 3000
          })
        break
        default:
          wx.showToast({
            title: '出错了！',
            icon: 'none',
            duration: 2000
          })
      }
    })
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target,
      operateId: e.currentTarget.dataset.id,
      isReply: e.currentTarget.dataset.isreply,
      operateIndex: e.currentTarget.dataset.index,
      isRight: e.currentTarget.dataset.isright
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      replying: false
    })
  },

  handleReply(){
    this.setData({
      replying: true
    })
  },


  replyInput(e){
    this.setData({
      replyContent: e.detail.value
    })
  },

  sendReply(){
    this.data.commentList[this.data.operateIndex].reply = this.data.replyContent
    this.data.commentList[this.data.operateIndex].replyDate = dateFormat(new Date(), 'yyyy-MM-dd hh:mm')
    this.data.commentList[this.data.operateIndex].isReply = true
    wx.cloud.callFunction({
      name: 'operateComment',
      data: {
        state: 'reply',
        id: this.data.operateId,
        content: this.data.replyContent
      }
    }).then(res => {
      this.setData({
        replyContent: ''
      })
    })
    this.setData({
      commentList: this.data.commentList,
      modalName: null,
      replying: false,
      isReply: true
    })
  },

  getDetailComment(page){
    wx.cloud.callFunction({
      name: 'getDetailComment',
      data: {
        voiceId: this.data.voice._id,
        authorId: this.data.voice.userInfo._openid,
        page
      }
    }).then(res => {
      if (res.result.length == 0) {
        this.setData({
          isDownLoading: false,
          loadingUp: false
        })
        return
      }
      let list = []
      list = res.result
      list.forEach((item, index) => {
        list[index].commentDate = dateFormat(item.commentDate, 'yyyy-MM-dd hh:mm')
        list[index].replyDate = dateFormat(item.replyDate, 'yyyy-MM-dd hh:mm')
        if (item.authorId == app.user._openid && item.reply == '') {
          list[index].isReply = false
        } else {
          list[index].isReply = true
        }
        if (item.commentatorId == app.user._openid) {
          list[index].showButton = false
        } else {
          list[index].showButton = true
        }
      })
      this.setData({
        commentList: this.data.commentList.concat(list),
        loadingUp: false
      })
      // console.log(this.data.commentList)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.voiceid && options.voiceid != ''){
      wx.cloud.callFunction({
        name: 'getDetailVoice',
        data: {
          voiceid: options.voiceid
        }
      }).then(res => {
        let obj = res.result
        obj.submitDate = timeTransform(obj.submitDate)
        obj.voiceContentAll = obj.voiceContent
        this.setData({
          voice: obj
        })
        this.getDetailComment(1)
      })
    }
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', (data)=> {
      this.setData({
        voice: data.voice
        // allContent: data.allContent,
        // bgImg: data.bgImg
      })
      this.getDetailComment(1)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.isDownLoading) return
    this.setData({
      loadingUp: true
    })
    this.data.currentPage++
    this.getDetailComment(this.data.currentPage)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})